# -*- encoding: utf-8 -*-
'''
file       :2_generate_pic.py
Description: 形成5*5个色块中只有中心点是白色, 而包围是黑色的图像
Date       :2022/12/21 14:52:58
Author     :Josco
version    :python3.7.8
'''
import numpy as np
import time
import cv2 as cv

index_list = [[5, 498, 266], [6, 411, 218], [7, 354, 185],[8,411,218]] #初始化index_list

def generate_pic(local_list):
    n, width, height = local_list[0], int(local_list[1] / 3), local_list[2]+4
    print(n, width, height)

    # # 这是五色的
    # point_color_list_1 = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
    # point_color_list_2 = np.array([0,0,0,0,0,0,1,1,1,0,0,0,0,0,0])

    # 这是七色的
    point_color_list_1 = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
    point_color_list_2 = np.array([0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0])
                        
    all_point_matrix = np.array([])
    len_list = int(len(list(point_color_list_1)) / 3)
    yushu = int(len_list / 2)
    
    for _row in range(int(height/3)):
        row_point_matrix = np.array([])
        for i in range(len_list):
            col_point_matrix = np.array([])
            for _col in range(width):
                if i != yushu:
                    col_point_matrix = np.hstack((col_point_matrix,point_color_list_1))
                else:
                    col_point_matrix = np.hstack((col_point_matrix,point_color_list_2))
            row_point_matrix = np.hstack((row_point_matrix, col_point_matrix))
        all_point_matrix = np.hstack((all_point_matrix, row_point_matrix))
    all_point_matrix = all_point_matrix.reshape((-1,width*n,3))
    all_point_matrix = all_point_matrix * 255

    all_point_matrix = all_point_matrix.astype("uint8")

    
    # 添加黑色包围
    black_margin_top = np.zeros((n,all_point_matrix.shape[1],3),dtype="uint8")
    all_point_matrix = np.concatenate((black_margin_top,all_point_matrix,black_margin_top),axis=0)
    black_margin_left = np.zeros((all_point_matrix.shape[0],n,3),dtype="uint8")
    all_point_matrix = np.concatenate((black_margin_left,all_point_matrix,black_margin_left),axis=1)
    # 添加白色包围
    white_margin_top = np.zeros((n,all_point_matrix.shape[1],3),dtype="uint8")
    white_margin_top = white_margin_top*255
    all_point_matrix = np.concatenate((white_margin_top,all_point_matrix,white_margin_top),axis=0)
    white_margin_left = np.zeros((all_point_matrix.shape[0],n,3),dtype="uint8")
    white_margin_left = white_margin_left*255
    all_point_matrix = np.concatenate((white_margin_left,all_point_matrix,white_margin_left),axis=1)

    n3_point_matrix = np.concatenate((all_point_matrix,all_point_matrix,all_point_matrix),axis=0)
    print(n, width, height)
    print(n3_point_matrix.shape)
    cv.imwrite("./standard_ori.bmp", n3_point_matrix)
    n9_point_matrix = np.concatenate((n3_point_matrix,n3_point_matrix,n3_point_matrix),axis=1)
    cv.imwrite("./"+ str(n) + "++standard.bmp", n9_point_matrix)

    show_pic("./"+ str(n) +"++standard.bmp", n=n)

def show_pic(pic_name, n=5, wait_key=10000):
    """
    desc: 将生成的图像挨个展示, 按10帧率的频率进行播放图像, 即1000/10
    param:
    pic_name : 文件路径
    n: 图像从bmp*n倍显示
    wait_key: 一张图片显示在屏幕的时间
    return:
        NONE
    """
    image_data = cv.imread(pic_name)
    h, w, c = image_data.shape
    resize_width = w
    resize_height = h
    windowname = "Image"    # 使用中文会乱码
    cv.namedWindow(windowname, cv.WINDOW_NORMAL)
    cv.resizeWindow(windowname, resize_width, resize_height)
    cv.imshow(windowname, image_data)
    wait_key=20000
    cv.waitKey(wait_key)
    # cv.destroyAllWindows()
    return None

def main():
    local_list = index_list[2]
    generate_pic(local_list)


if __name__ == "__main__":
    main()